<template>
  <div>
    <van-cell title="昵称" is-link :value="user?.username" @click="toEdit('username', '昵称', user?.username)" />
    <van-cell title="电话" is-link :value="user?.phone" @click="toEdit('phone', '电话', user?.phone)" />
    <van-cell title="邮件" is-link :value="user?.email" @click="toEdit('email', '邮件', user?.email)" />
<!--    <van-cell title="性别" is-link :value="user?.gender" @click="toEdit('gender', '性别', user?.gender)" />-->
    <van-cell title="性别" is-link :value="user?.gender === 0 ? '男' : '女' " @click="toEdit('gender', '性别', user?.gender)" />
    <van-cell title="账户" is-link :value="user?.userAccount" @click="toEdit('userAccount', '账户', user?.userAccount)" />
    <van-cell title="头像" is-link :value="user?.avatarUrl" @click="toEdit('avatarUrl', '头像', user?.avatarUrl)">
      <img style="height: 45px" :src="user?.avatarUrl" />
    </van-cell>
<!--    <van-cell title="状态" is-link :value="user?.userStatus" @click="toEdit('userStatus', '状态', user?.userStatus)" />-->
    <van-cell title="星球编号" is-link :value="user?.planetCode" @click="toEdit('planetCode', '星球编号', user?.planetCode)" />
    <van-cell title="注册时间" is-link :value="user?.createTime" @click="toEdit('createTime', '注册时间', user?.createTime)" />

<!--    <van-cell title="修改信息" is-link to="/user/update" />-->
    <van-cell title="我创建的队伍" is-link to="/team/create" />
    <van-cell title="我加入的队伍" is-link to="/team/join" />
    <van-cell
        title="退出登录"
        is-link
        @click="handleLogout"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import {showSuccessToast, showFailToast, showConfirmDialog} from 'vant';
import { getCurrentUser } from '../services/user';
import {userType} from "../assets/user";
import myAxios from "../plugins/myAxios";

const router = useRouter();


// 用户数据
const user = ref({
  username: '',
  phone: '',
  email: '',
  gender: '',
  userAccount: '',
  avatarUrl: '',
  userStatus: '',
  planetCode: '',
  createTime: '',
});



// 获取当前用户信息
onMounted(async () => {
  try {
    const res = await getCurrentUser();
    if (res) {
      user.value = res;
      console.log('请求成功:', res);
      showSuccessToast('获取用户信息成功');
    } else {
      console.log('请求错误:', res);
      showFailToast('获取用户信息失败');
    }
  } catch (error) {
    console.error('请求错误:', error);
    showFailToast('获取用户信息失败');
  }
});

// 跳转到编辑页面
const toEdit = (editKey: string, editName: string, currentValue: string) => {
  router.push({
    path: '/user/edit',
    query: {
      editKey,
      editName,
      currentValue,
    },
  });
};


// 退出登录方法
const handleLogout = async () => {
  try {
    // 显示确认对话框
    await showConfirmDialog({
      title: '确认退出',
      message: '您确定要退出当前账号吗？',
    });

    // 执行退出操作（根据实际接口调整）
    await myAxios.post('/user/logout');

    // 跳转到登录页
    router.push('/user/login');
  } catch (error) {
    // 用户取消退出或请求失败
    if (error !== 'cancel') {
      console.error('退出失败:', error);
      showFailToast('退出失败，请重试');
    }
  }
};


</script>

<style scoped>
/* 样式可以根据需要添加 */
</style>